package com.zut.chat.controller;

import com.zut.chat.entity.MessageRateLimitConfig;
import com.zut.chat.service.MessageRateLimitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息频率限制管理控制器
 */
@RestController
@RequestMapping("/api/rate-limit")
@CrossOrigin(origins = "*")
public class MessageRateLimitController {

    @Autowired
    private MessageRateLimitService messageRateLimitService;

    /**
     * 获取所有频率限制配置
     */
    @GetMapping("/configs")
    public ResponseEntity<Map<String, Object>> getAllConfigs() {
        try {
            List<MessageRateLimitConfig> configs = messageRateLimitService.getAllConfigs();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", configs);
            response.put("message", "获取频率限制配置成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取频率限制配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 根据用户类型获取频率限制配置
     */
    @GetMapping("/config/{userType}")
    public ResponseEntity<Map<String, Object>> getConfigByUserType(@PathVariable String userType) {
        try {
            MessageRateLimitConfig config = messageRateLimitService.getConfigByUserType(userType);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", config);
            response.put("message", "获取频率限制配置成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取频率限制配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 更新频率限制配置
     */
    @PutMapping("/config/{id}")
    public ResponseEntity<Map<String, Object>> updateConfig(@PathVariable Long id, @RequestBody MessageRateLimitConfig config) {
        try {
            config.setId(id);
            boolean success = messageRateLimitService.updateConfig(config);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? "更新频率限制配置成功" : "更新频率限制配置失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "更新频率限制配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 创建新的频率限制配置
     */
    @PostMapping("/config")
    public ResponseEntity<Map<String, Object>> createConfig(@RequestBody MessageRateLimitConfig config) {
        try {
            boolean success = messageRateLimitService.save(config);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? "创建频率限制配置成功" : "创建频率限制配置失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "创建频率限制配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 删除频率限制配置
     */
    @DeleteMapping("/config/{id}")
    public ResponseEntity<Map<String, Object>> deleteConfig(@PathVariable Long id) {
        try {
            boolean success = messageRateLimitService.removeById(id);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? "删除频率限制配置成功" : "删除频率限制配置失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "删除频率限制配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 清理过期的用户消息频率记录
     */
    @PostMapping("/cleanup")
    public ResponseEntity<Map<String, Object>> cleanupExpiredRecords() {
        try {
            messageRateLimitService.cleanupExpiredRecords();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "清理过期记录成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "清理过期记录失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 